package com.bilibili.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.service.BackDataService;
import com.bilibili.service.impl.BackDataServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
@WebServlet("/backData.do")
public class BackDataServlet extends HttpServlet {
    BackDataService bds = new BackDataServiceImpl();
    ObjectMapper objectMapper = new ObjectMapper(); 
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");
        
        String action = req.getParameter("action");
        String timeRange = req.getParameter("timeRange");
        
        Map<String, Object> result = new HashMap<>();
        
        if ("videoTrend".equals(action)) {
            Map<String, Object> count = bds.selectCount();
            Map<String, Object> tables = bds.selectTables(timeRange);
            result.putAll(count);
            result.putAll(tables);
        }
        
        objectMapper.writeValue(resp.getWriter(), result);
    }
}
